import React from 'react';
import Header from '../../components/header';
import {connect} from 'react-redux';
import { changePage, getUser,logout } from '../../store/actions'
import {message,Row,Col} from 'antd'


class Access extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            title:'用户登录',
            page:'login'
        }
    }
    static contextTypes = {
        router: React.PropTypes.object
    };
    componentWillMount(){
        if(window.sessionStorage){
            const session_user_data = window.sessionStorage.getItem('user'),
              _info =JSON.parse(session_user_data);
            if(this.props.user.accessToken == '' && session_user_data){
                this.props.dispatch(getUser({content:_info}));
                if(_info.user.username == 'systemadmin'){
                    this.context.router.push('/_admin')
                }else if(_info.user.type !== 2){
                    this.context.router.push('/images')
                }else{
                    const ispass = _info.user.ispass,
                      specialstatus = _info.user.specialstatus,
                      status = _info.user.status,
                      _this= this;
                    switch(ispass){
                        case 0://没审核（初始状态）
                            switch(specialstatus){
                                case 0://没填资料
                                    message.warning('请尽快有完善个人资料',1);
                                    _this.context.router.push('/reg-spec-form?step=1');
                                    break;
                                case 1://填过资料
                                default:
                                    message.info('正在审核您的申请，请耐心等待',1);
                                    _this.context.router.push('/reg-spec-form?step=2&status=0');
                                    break;
                            }
                            break;
                        case 1://通过
                            switch (status){
                                case 1://启用
                                    message.success('专家-登录成功',1);
                                    _this.context.router.push('/work-board');
                                    break;
                                case 0://停用
                                default:
                                    message.error('您的账号已被冻结，请联系您的管理员！',2);
                                    //_this.props.dispatch(logout());
                                    break;
                            }
                            break;
                        case 2://拒绝
                            message.error('您的申请被拒绝了，请联系您的管理员！',2);
                            _this.context.router.push('/reg-spec-form?step=2&status=1');
                            break;
                    }
                }
            }
        }else{this.context.router.push('/login');}
    }
    backHome(e){
        e.preventDefault();
        const _this = this,
          token = _this.props.user.accessToken;

        if(token && token !== ''){
            console.log('token:',token)
            _this.props.dispatch(logout(function(){
                _this.context.router.push('login')
                console.log('退出成功')
            },function(){
                console.log('退出失败')
            }))
        }else{
            _this.context.router.push('login')
            console.log('返回首页')
        }
    }
    render(){
        return(
            <div id="access">
                <Row style={{backgroundColor: '#fff'}}>
                    <Col  xs={{span:24,offset:0}}
                          sm={{span:24,offset:0}}
                          md={{span:22,offset:1}}
                          lg={{span:20,offset:2}}>
                        <h2 id="accessTitle">互联网影像数据中心
                            <a style={{
                                lineHeight: '24px',
                                fontSize: '12px',
                                margin: '0 0 0 10px'
                            }} href="../index.html"
                               onClick={this.backHome.bind(this)}
                            >返回首页</a>
                        </h2>
                    </Col>
                </Row>
                <Row style={{backgroundColor: '#e5e8ef'}}>
                    <Col  xs={{span:24,offset:0}}
                          sm={{span:24,offset:0}}
                          md={{span:22,offset:1}}
                          lg={{span:20,offset:2}}>
                        {this.props.children}
                    </Col>
                </Row>

                <div style={{
                    padding:'10px 0',
                    textAlign: 'center',
                    fontSize: '14px',
                    color: '#999'
                }}>Copyright©亿阳集团 版权所有   地址：北京市海淀区杏石口路99号A座</div>
            </div>
        )
    }
}

function mapStateToProps(state){
    const {_pages,user} = state.rootReducer;
    const {pathname} = state.routing.locationBeforeTransitions;
    return {_pages,user,pathname}
}
export default connect(mapStateToProps)(Access)